import { Behaviour, Transform, getGameObjectById, GameObject } from "./gameengine";
import { ColliderSystem } from "./collider/colliderSystem";
import { Rect_Collider, RectColliderType } from "./collider/rect_Collider";
import { SceneFollow } from "./SceneFollow/sceneFollow";
import { AnimationRenderer } from "./AnimationRenderer";
import { ConversationTest } from "./conversation/conversationTest";
import { PickItemBehaviour, DoorBehaviour, HiddenClueBehaviour, DisappearBehaviour } from "./item/item";

export type speeds = {
    x: number,
    y: number;
}

export class MainRoleBehaviour extends Behaviour {

    LEFT: number = 37;
    UP: number = 38;
    RIGHT: number = 39;
    DOWN: number = 40;

    FRONT_OFFSET: number = 0;
    LEFT_OFFSET: number = 4;
    RIGHT_OFFSET: number = 8;
    BACK_OFFSET: number = 12;

    SPACE: number = 32;

    SPEED: number = 0;  //人物行走速度

    private speedX = 0;
    private speedY = 0;

    private sceneFollow: SceneFollow;
    private animationRender: AnimationRenderer;

    private onKeyDown: (ev) => void;

    private lastInteractionObject: GameObject;

    public getLastInteractionObject() {
        return this.lastInteractionObject;
    }

    public setLastInteractionObject(gameObject: GameObject) {
        if (gameObject) {
            this.lastInteractionObject = gameObject;
        }

    }


    protected onStart() {
        this.SPEED = 2;     //设置角色初始行走速度

        this.sceneFollow = getGameObjectById("Scene").getScript(SceneFollow);
        this.animationRender = getGameObjectById("mainRole").getScript(AnimationRenderer);

        this.onKeyDown = ev => {

            const itemColliders = getGameObjectById("Scene").getScript(ColliderSystem).itemColliders;
            const item = itemColliders[0];

            // console.log(ev.keyCode);
            switch (ev.keyCode) {
                case this.DOWN:
                    this.animationRender.isIdle = false;
                    this.animationRender.currentOffset = this.FRONT_OFFSET;
                    this.speedY = this.SPEED;
                    this.speedX = 0;
                    ev.preventDefault();
                    if (!item) {
                        break;
                    }
                    if (item.isDoor == true) {
                        const doorBehaviour = getGameObjectById(item.id).getScript(DoorBehaviour);
                        // console.log('这是门');
                        doorBehaviour.ChangeScene();
                    }
                    break;
                case this.UP:
                    this.animationRender.isIdle = false;
                    this.animationRender.currentOffset = this.BACK_OFFSET;
                    this.speedY = -this.SPEED;
                    this.speedX = 0;
                    ev.preventDefault();
                    if (!item) {
                        break;
                    }
                    if (item.isDoor == true) {
                        const doorBehaviour = getGameObjectById(item.id).getScript(DoorBehaviour);
                        // console.log('这是门');
                        doorBehaviour.ChangeScene();
                    }
                    break;
                case this.LEFT:
                    this.animationRender.isIdle = false;
                    this.animationRender.currentOffset = this.LEFT_OFFSET;
                    this.speedX = -this.SPEED;
                    this.speedY = 0;
                    ev.preventDefault();
                    break;
                case this.RIGHT:
                    this.animationRender.isIdle = false;
                    this.animationRender.currentOffset = this.RIGHT_OFFSET;
                    this.speedX = this.SPEED;
                    this.speedY = 0;
                    ev.preventDefault();
                    break;
                case this.SPACE:
                    console.log('交互！');

                    if (!item) {
                        break;
                    }

                    if (item.canTalk == true) {
                        const conversationTest = getGameObjectById(item.id).getScript(ConversationTest);
                        // this.lastInteractionObject=conversationTest.displayObject;
                        this.setLastInteractionObject(conversationTest.displayObject);
                        lastTouch.obj = conversationTest.displayObject;

                        lastTouchGameObject = conversationTest.displayObject;

                        conversationTest.searchKeys(item.clue);
                        // console.log(item.clue);
                        console.log('对话物品');
                    }
                    if (item.canPick == true) {
                        const pickItemBehaviour = getGameObjectById(item.id).getScript(PickItemBehaviour);
                        // console.log(pickItemBehaviour);
                        if (!pickItemBehaviour) {
                            break;
                        }
                        pickItemBehaviour.PickItem();
                        // console.log(user.keys);
                        console.log('拾取物品');
                    }
                    if (item.isHiddenClue == true) {
                        const hiddenClueBehaviour = getGameObjectById(item.id).getScript(HiddenClueBehaviour);
                        if (!hiddenClueBehaviour) {
                            break;
                        }
                        hiddenClueBehaviour.PickItem();
                        console.log('拾取物品');
                    }
                    if (item.isDoor == true) {
                        const doorBehaviour = getGameObjectById(item.id).getScript(DoorBehaviour);
                        // this.lastInteractionObject=doorBehaviour.displayObject;
                        this.setLastInteractionObject(doorBehaviour.displayObject);
                        lastTouch.obj = doorBehaviour.displayObject;

                        lastTouchGameObject = doorBehaviour.displayObject;

                        doorBehaviour.searchKeys(item.clue);
                        // doorBehaviour.ChangeScene();
                        console.log("开锁操作");
                    }
                    if (item.isDisappear == true) {
                        const disappearBehaviour = getGameObjectById(item.id).getScript(DisappearBehaviour);
                        disappearBehaviour.RemoveItem();
                        console.log("消失吧！",item.id);
                    }
                    break;

            }
        };

        this.addOnKeyDownListener();

        window.onkeyup = (ev) => {
            this.animationRender.isIdle = true;
            switch (ev.keyCode) {
                case this.DOWN:
                case this.UP:
                    this.speedY = 0;
                    ev.preventDefault();
                    break;
                case this.LEFT:
                case this.RIGHT:
                    this.speedX = 0;
                    ev.preventDefault();
                    break;
                case this.SPACE:
                    ev.preventDefault();
                    break;
            }
        };

    }
    protected onUpdate(advancedTime: number) {

        // if(!this.lastInteractionObject){
        //     console.log("this.lastInteractionObject没了！！！！");
        //     this.lastInteractionObject=getGameObjectById("bed7");
        // }

        const transform = this.displayObject.getScript(Transform);
        const colliderSystem = getGameObjectById("Scene").getScript(ColliderSystem);
        const collider = this.displayObject.getScript(Rect_Collider);

        const nextCollider = {
            left: collider.left + this.speedX,
            right: collider.right + this.speedX,
            top: collider.top + this.speedY,
            bottom: collider.bottom + this.speedY
        };

        // if (DEBUG){
        //     debugger
        // }

        const hitTestWallResult = colliderSystem.check(nextCollider, RectColliderType.WALL);
        const hitTestItemResult = colliderSystem.check(nextCollider, RectColliderType.ITEM);

        //结果为false说明可以行走，否则为碰撞
        if (!hitTestWallResult && !hitTestItemResult) {   
            transform.x += this.speedX;
            transform.y += this.speedY;
        }
    }

    public getSpeed() {
        const characterSpeed: speeds = {
            x: this.speedX,
            y: this.speedY
        };
        return characterSpeed;
    }

    public addOnKeyDownListener() {
        window.addEventListener("keydown", this.onKeyDown);
    }

    public removeOnKeyDownListener() {
        window.removeEventListener("keydown", this.onKeyDown);
    }

}

declare var DEBUG: boolean;

export let lastTouchGameObject: GameObject;

export class LastTouchGameObject {
    public obj: GameObject;
}

export const lastTouch = new LastTouchGameObject();